#pragma once
#include "../TcpServer.hpp"

class EchoServer
{
public:
	EchoServer(int port)
		: _tcpserver(port)
	{
		_tcpserver.SetThreadCount(2);
		_tcpserver.EnableInactiveRelease(10);
		_tcpserver.SetMessageCallback(std::bind(&EchoServer::OnMessage,this,std::placeholders::_1,std::placeholders::_2));
		_tcpserver.SetCloseCallback(std::bind(&EchoServer::OnConnextDis,this,std::placeholders::_1));
		_tcpserver.SetConnectCallback(std::bind(&EchoServer::OnConnect,this,std::placeholders::_1));
	}
	void OnConnextDis(const Ptrconnection &con)
	{
		DEL_LOG("close connection : %p", con->GetContext());
	}
	void OnConnect(const Ptrconnection &con)
	{
		INF_LOG("new connection : %p", con->GetContext());
	}

	void OnMessage(const Ptrconnection &con, Buffer *buff)
	{
		// DEL_LOG("%s", buff->ReadPosition());
		con->Send(buff->ReadPosition(), buff->ReadableSize());
		buff->MoveReadOffset(buff->ReadableSize());
		con->Shutdown();
	}
	void Strat()
	{
		_tcpserver.Start();
	}

private:
	TcpServer _tcpserver;
};